<template>
  <el-dialog :title="title" @close="showFalse" :visible="showDialog">
    <el-form :model="form" ref="form" :rules="rules">
      <el-form-item label="部门名称" prop="name" label-width="120px">
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="部门编码" prop="code" label-width="120px">
        <el-input v-model="form.code" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="活动负责人" prop="manager" label-width="120px">
        <el-select
          style="width:100%;"
          v-model="form.manager"
          @focus="addState"
          placeholder="请选择"
        >
          <el-option
            v-for="item in addList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce" label-width="120px">
        <el-input
          type="textarea"
          :rows="4"
          v-model="form.introduce"
          autocomplete="off"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="showFalse">取 消</el-button>
      <el-button type="primary" @click="addDept">确 定</el-button>
    </div>
    
  </el-dialog>
</template>

<script>
import { getGepartment, addDepts, editQuery, editState } from '@/api/department'
import { getSimple } from '@/api/employees'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    pid: {
      type: String,
      require: true
    }
  },
  methods: {
    showFalse () {
      this.form = {}
      this.$refs.form.resetFields()
      this.$emit('update:showDialog', false)
    },
    async getDetail (id) {
      let res = await editQuery(id)
      // 把获取到的数据赋值给当前的form数据
      // 因为form这个属性跟表单元素做了双向绑定
      this.form = res.data
      this.oldForm.name = this.form.name
      this.oldForm.code = this.form.code
    },
    async addState () {
      let res = await getSimple()
      this.addList = res.data
    },
    addDept () {
      this.$refs.form.validate(async v => {
        if (v) {
          if (this.form.id) {
            await editState(this.form)
            this.$message.success('修改信息成功')
          } else {
            await addDepts({
              ...this.form,
              pid: this.pid
            })
            this.$message.success('新增部门成功')
          }
          this.$emit('ADD')
          this.$emit('update:showDialog', false)
        }
      })
    }
  },
  data () {
    let checkName = async (rules, value, callback) => {
      let res = await getGepartment()
      if (this.form.id) {
        let list = res.data.depts
        list.filter(item => item.pid == this.form.pid)
        let repeat = list.some(
          item => item.name == value && item.name != this.oldForm.name
        )
        repeat
          ? callback(new Error('不能存在相同的部门名称,请修改!'))
          : callback()
      } else {
        let list = res.data.depts
        list.filter(item => item.pid == this.pid)
        for (let i = 0; i < list.length; i++) {
          if (list[i].name == value) {
            return callback(new Error('不能存在相同部门，请修改'))
          }
        }
        callback()
      }
    }
    let checkCode = async (rules, value, callback) => {
      let res = await getGepartment()
      let repeat
      if (this.form.id) {
        repeat = res.data.depts.some(
          item => item.code == value && item.code != this.oldForm.code
        )
      } else {
        repeat = res.data.depts.some(item => item.code == value)
      }
      repeat ? callback(new Error('已存在相同部门编码，请修改')) : callback()
    }
    return {
      oldForm: {
        name: '',
        code: ''
      },
      addList: [],
      form: {},
      rules: {
        name: [
          { required: true, message: '请输入', trigger: 'blur' },
          { min: 1, max: 50, message: '内容在1-50长度', trigger: 'blur' },
          { validator: checkName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入', trigger: 'blur' },
          { min: 1, max: 50, message: '内容在1-50长度', trigger: 'blur' },
          { validator: checkCode, trigger: 'blur' }
        ],
        manager: [{ required: true, message: '请输入', trigger: 'blur' }],
        introduce: [
          { required: true, message: '请输入', trigger: 'blur' },
          { min: 1, max: 300, message: '内容在1-300长度', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    title () {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  }
}
</script>

<style lang="scss">
.dialog-footer {
  display: flex;
  justify-content: center;
}
.el-dialog__header {
  background-color: #f09;
}
</style>
